package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Asset;
import com.example.assetmanagement.domain.AssetHistory;
import com.example.assetmanagement.service.AssetService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/asset")
@RequiredArgsConstructor
public class AssetController {

    private final AssetService assetService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<Asset>> list(@Valid PageRequest pageRequest,
                                                 @RequestParam(required = false) String name,
                                                 @RequestParam(required = false) Long categoryId,
                                                 @RequestParam(required = false) Integer status) {
        return ApiResponse.success(assetService.list(pageRequest, name, categoryId, status));
    }

    @GetMapping("/{id}")
    public ApiResponse<Asset> detail(@PathVariable Long id) {
        return ApiResponse.success(assetService.getById(id));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody @Valid Asset asset) {
        assetService.add(asset);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody @Valid Asset asset) {
        assetService.update(id, asset);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        assetService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        assetService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    // 导入
    @PostMapping("/import")
    public ApiResponse<?> importAssets(@RequestPart("file") MultipartFile file) throws IOException {
        assetService.importAssets(file);
        return ApiResponse.success(null);
    }

    // 导出
    @GetMapping("/export")
    public void exportAssets(HttpServletResponse response,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false) Long categoryId,
                             @RequestParam(required = false) Integer status) throws IOException {
        byte[] data = assetService.exportAssets(name, categoryId, status);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        String fileName = URLEncoder.encode("assets.xlsx", StandardCharsets.UTF_8);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
        response.getOutputStream().write(data);
    }

    // 获取历史记录
    @GetMapping("/{assetId}/history")
    public ApiResponse<List<AssetHistory>> history(@PathVariable Long assetId) {
        return ApiResponse.success(assetService.getHistoryByAsset(assetId));
    }

    // 新增历史记录
    @PostMapping("/history")
    public ApiResponse<?> addHistory(@RequestBody AssetHistory history) {
        assetService.addHistory(history);
        return ApiResponse.success(null);
    }
} 